<?php

namespace addons\xpframework\library\xcore\xcore\utils;

class XcMapUtils
{

    public static function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $EARTH_RADIUS = 6378.137;

        $radLat1 = static::rad($lat1);
        $radLat2 = static::rad($lat2);
        $a = $radLat1 - $radLat2;
        $b = static::rad($lng1) - static::rad($lng2);
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $EARTH_RADIUS;
        $s = round($s * 10000) / 10000;

        return $s * 1000;
    }
    
    private static function rad($d)
    {
        return $d * M_PI / 180.0;
    }
}
